extends CenterContainer




onready var item_box = $PanelContainer/VBoxContainer/itemBox

onready var name_input = $PanelContainer/VBoxContainer/name/LineEdit
onready var cache_op = $PanelContainer/VBoxContainer/cache/OptionButton
onready var space_op = $PanelContainer/VBoxContainer/space/OptionButton
onready var correlation_op = $PanelContainer/VBoxContainer/correlation/OptionButton

var edit_data = null

var poke_data

var path = ""


signal close()
	

func set_edit_data(edit_data):
	self.edit_data = edit_data
	
	var map_register_list = DataSource.get_data_list(DataSource.map_register)
	
	
	
	if edit_data:
		name_input.text = edit_data["name"]
		cache_op.select(cache_op.get_item_index(edit_data["cache"]))
		space_op.select(space_op.get_item_index(edit_data["space"]))
		
		for i in map_register_list:
			if i["id"] != edit_data["id"]:
				correlation_op.add_item(i["name"], i["id"])
				
		var correlation_index = correlation_op.get_item_index(edit_data.get("correlation", 0))
		correlation_op.select(correlation_index)
		
	else:
		for i in map_register_list:
			correlation_op.add_item(i["name"], i["id"])

# 设置选择资源路径
func set_tscn_path(path):
	self.path = path
#	$PanelContainer/VBoxContainer/path/Label.text = "path: %s" % path
	$PanelContainer/VBoxContainer/TextEdit.text = path
	pass
	
	
func _on_cancel_button_up():
	MainTool.getUILayer().pop_ui()


func _on_ok_button_up():
	var path_change = false
	var temp_path = $PanelContainer/VBoxContainer/TextEdit.text
	if self.path != temp_path:
		path_change = true
	self.path = temp_path
	if edit_data:
		var one = DataSource.get_data_by_one(DataSource.map_register, path, "path")
		if one and path_change:
			MainTool.toast_info("该地图已经存在，无法注册")
			return
		read_form(edit_data)
			
		DataSource.update_data(DataSource.map_register, edit_data)
		
		MainTool.toast_info("更新成功!")
	else:
		var data = {
			"path": path,
		}
		
		read_form(data)
		
		DataSource.add_data(DataSource.map_register, data)
		MainTool.toast_info("添加成功!")
	
	MainTool.getUILayer().pop_ui()
	
	emit_signal("close")


func read_form(dic):
	dic["name"] = name_input.text
	dic["path"] = path
	var path_arr = path.split("/")
	dic["enName"] = path_arr[path_arr.size()-1].replace(".tscn", "")
	
	dic["cache"] = cache_op.get_selected_id()
	dic["space"] = space_op.get_selected_id()
	dic["correlation"] = correlation_op.get_selected_id()
